﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBO.Data;
using PBO.Game;
using PBO.Game.BattleLog;

namespace PBO.Game.BattleClass
{
    public partial class Battle
    {
        private void HurtTarget(Pokemon user, Pokemon target, ref int damage, Move selMove, bool moveCall, bool hpMax)
        {
            var selMoveType = GetAttackMoveType(selMove, user);

            if (target.BattleState.substituted)
            {
                if (damage > target.BattleState.subHP)
                    damage = target.BattleState.subHP;
                log.AppendMsg("HurtSubstitute", target.GetNameString());
                //log.AddText(target.GetNameString() + "的替身代替它受到攻击！");
                target.SubGetHurt(Convert.ToInt32(damage));

                if (selMove.Data.AddEffect1 == MoveAdditionalEffect.自爆)
                {
                    user.HurtFraction(1);
                }
                if (user.GetItem() == Item.ShellBell && user.HealthPower != user.MaxHp && user.HealthPower != 0)
                {
                    user.HPRecover(damage * 0.125);
                    log.AppendMsg("ItemRecover", user.CustomName);
                    //log.AppendText(user.CustomName + "的空贝铃使它回复了HP！");
                }
                if (!target.BattleState.substituted)
                {
                    log.AppendMsg("DeSubstitute", target.GetNameString());
                    //log.AddText(target.GetNameString() + "的替身被打消了！");
                    if (selMove.Data.AddEffect1 == MoveAdditionalEffect.暂时束缚)
                    {
                        target.BattleState.constraint = true;
                        if (user.GetItem() == Item.GripClaw)
                        {
                            target.BattleState.constraintTurn = 8;
                        }
                        else
                        {
                            target.BattleState.constraintTurn = _random.Next(5, 7);
                        }
                        target.BattleState.constraintCounter = 0;
                        target.BattleState.constraintBy = GetPokemonIndex(user);
                        log.AppendMsg("EnCantSelectWithdraw", target.CustomName);
                        //log.AppendText(target.CustomName + "不能逃走了！");
                    }
                }
            }
            else
            {
                if (target.BattleState.endure && damage >= target.HealthPower)
                {
                    damage = target.HealthPower - 1;
                }
                if (target.GetItem() == Item.FocusBand && damage >= target.HealthPower && this.OneNth(10))
                {
                    damage = target.HealthPower - 1;
                    log.AppendMsg("FocusItem", target.GetNameString(), Item.FocusBand);
                    //log.AddText(target.GetNameString() + "的气息头巾使它留下了1HP！");
                }
                else if (target.GetItem() == Item.FocusSash && hpMax && damage >= target.HealthPower)
                {
                    damage = target.HealthPower - 1;
                    log.AppendMsg("FocusItem", target.GetNameString(), Item.FocusSash);
                    //log.AddText(target.GetNameString() + "的气息腰带使它留下了1HP！");
                    target.UseItem();
                }
                else if (target.GetItem().IsHalfHurtBerry(selMoveType))
                {
                    double typeEff = CalculateAttackTypeEffect(user, target, selMoveType);
                    if (typeEff >= 2)
                    {
                        damage = Convert.ToInt32(damage * 0.5);
                        target.RaiseItem();
                    }
                }
                if (damage > target.HealthPower)
                    damage = target.HealthPower;
                log.AppendLog("(对" + target.GetNameString() + "造成" + damage + "伤害)");
                target.GetHurt(damage);
                target.turnHurt.Add(damage);
                target.turnHurtMove.Add(selMove.Data.Id);
                target.turnHurtBy.Add(GetPokemonIndex(user));
                if (target.BattleState.bide)
                {
                    target.BattleState.bideHurt += damage;
                    target.BattleState.bideTarget = GetPokemonIndex(user);
                }
                user.attackSuccessed = true;

                if (selMove.Data.AddEffect1 == MoveAdditionalEffect.自爆)
                {
                    user.HurtFraction(1);
                }
                if (user.GetItem() == Item.ShellBell && user.HealthPower != user.MaxHp && user.HealthPower != 0)
                {
                    user.HPRecover(damage * 0.125);
                    log.AppendMsg("ItemRecover", user.CustomName, Item.ShellBell);
                    //log.AppendText(user.CustomName + "的空贝铃使它回复了HP！");
                }

                if (target.GetSelTrait() == Trait.ColorChange && target.HealthPower != 0 && target.Type1 != selMoveType)
                {
                    target.Type1 = selMoveType;
                    log.AppendMsg("TypeChange", target.CustomName, selMoveType);
                    //log.AddText(target.CustomName + "的属性变成了" + selMoveType.ToCnName() + "！");
                }

                if (user.HealthPower != 0 && selMove.Contact)
                {
                    switch (target.GetSelTrait())
                    {
                        case Trait.Aftermath:
                            if (target.HealthPower == 0 && !Ground.Damp && user.GetSelTrait() != Trait.MagicGuard)
                            {
                                log.AppendMsg("Ability", target.CustomName, target.GetSelTrait());
                                log.AppendMsg("Hurt", user.CustomName);
                                //log.AppendText(target.CustomName + "的引爆特性使" + user.CustomName + "受到了伤害！");
                                user.HurtFraction(4);
                            }
                            break;
                        case Trait.RoughSkin:
                            if (user.GetSelTrait() != Trait.MagicGuard)
                            {
                                log.AppendMsg("Ability", target.CustomName, target.GetSelTrait());
                                log.AppendMsg("Hurt", user.CustomName);
                                //log.AppendText(target.CustomName + "的鲨鱼皮特性使" + user.CustomName + "受到了伤害！");
                                user.HurtFraction(8);
                            }
                            break;
                        case Trait.Static:
                            if (this.RandomHappen(30))
                            {
                                log.AppendMsg("Ability", target.CustomName, target.GetSelTrait());
                                ChangeState(user, PokemonState.Paralysis, this.log);
                                //log.AppendText(target.CustomName + "的静电特性使" + user.CustomName + "麻痹了！");
                            }
                            break;
                        case Trait.PoisonPoint:
                            if (this.RandomHappen(30))
                            {
                                log.AppendMsg("Ability", target.CustomName, target.GetSelTrait());
                                ChangeState(user, PokemonState.Poison, this.log);
                                //log.AppendText(target.CustomName + "的毒针特性使" + user.CustomName + "中毒了！");
                            }
                            break;
                        case Trait.EffectSpore:
                            log.AppendMsg("Ability", target.CustomName, target.GetSelTrait());
                            EffectSpore(user);
                            //log.AppendText(target.CustomName + "的孢子特性使" + user.CustomName + "中毒了！");
                            //log.AppendText(target.CustomName + "的孢子特性使" + user.CustomName + "麻痹了！");
                            //log.AppendText(target.CustomName + "的孢子特性使" + user.CustomName + "睡着了！");
                            break;
                        case Trait.FlameBody:
                            if (this.RandomHappen(30))
                            {
                                log.AppendMsg("Ability", target.CustomName, target.GetSelTrait());
                                ChangeState(user, PokemonState.Burn, this.log);
                                //log.AppendText(target.CustomName + "的火焰之身特性使" + user.CustomName + "烧伤了！");
                            }
                            break;
                        case Trait.CuteCharm:
                            if (this.RandomHappen(30))
                            {
                                log.AppendMsg("Ability", target.CustomName, target.GetSelTrait());
                                user.Captivate(target, true);
                            }
                            break;
                    }
                    if (target.GetItem() == Item.StickyBarb && user.GetItem() == Item.None && this.OneNth(10))
                    {
                        user.SetItem(Item.StickyBarb);
                        target.SetItem(Item.None);
                        //log.AppendText(target.CustomName + "的附针转移到了" + user.CustomName + "身上！");
                    }
                }

                if (target.HealthPower != 0)
                {
                    if ((user.GetItem() == Item.KingsRock || user.GetItem() == Item.RazorFang) && selMove.KingRock && this.OneNth(10))
                        target.terrify(user);

                    if (target.GetItem() == Item.EnigmaBerry)
                    {
                        double typeEff = CalculateAttackTypeEffect(user, target, selMoveType);
                        if (typeEff >= 2)
                            target.RaiseItem();
                    }
                    if (target.BattleState.rage)
                    {
                        target.AbilityLvUpChanged(StatType.Atk, 1, false);
                        //target.AtkLVUp(1, false);
                    }
                }
                if (target.GetItem() == Item.JabocaBerry && selMove.Data.Category == MoveCategory.物理)
                {
                    log.AppendMsg("ReHurtItem", user.CustomName, target.CustomName, Item.JabocaBerry);
                    //log.AddText(target.CustomName + "的63号果实使" + user.CustomName + "受到了伤害！");
                    user.GetHurt(damage * 0.125);
                    target.UseItem();
                }
                else if (target.GetItem() == Item.RowapBerry && selMove.Data.Category == MoveCategory.特殊)
                {
                    log.AppendMsg("ReHurtItem", user.CustomName, target.CustomName, Item.RowapBerry);
                    //log.AddText(target.CustomName + "的64号果实使" + user.CustomName + "受到了伤害！");
                    user.GetHurt(damage * 0.125);
                    target.UseItem();
                }
                CalculateMoveEffect(user, target, selMove, moveCall);
            }
        }

        #region EffectSpore

        public static void ChangeState(Pokemon pm, PokemonState state, ILogBuilder log)
        {
            pm.SetState(state, null, false);

            switch (state)
            {
                case PokemonState.Poison:
                    log.AppendMsg("EnPSN", pm.CustomName);
                    break;
                case PokemonState.Toxin:
                    log.AppendMsg("EnBadlyPSN", pm.CustomName);
                    break;
                case PokemonState.Burn:
                    log.AppendMsg("EnBRN", pm.CustomName);
                    break;
                case PokemonState.Paralysis:
                    log.AppendMsg("EnPAR", pm.CustomName);
                    break;
                case PokemonState.Freeze:
                    log.AppendMsg("EnFRZ", pm.CustomName);
                    break;
                case PokemonState.Sleep:
                    log.AppendMsg("EnSLP", pm.CustomName);
                    break;
            }
        }

        private void EffectSpore(Pokemon pm)
        {
            PokemonState[] states = { PokemonState.Poison, PokemonState.Paralysis, PokemonState.Sleep };
            int rand = _random.Next(states.Length);
            var state = states[rand];
            ChangeState(pm, state, this.log);
        }

        #endregion

    }
}
